package wysiwyg.shapes
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Button;
	import mx.controls.TextArea;
	
	import wysiwyg.Options.TextOptions;
	import wysiwyg.objectHandles.RectangleHandles;

	public class TextShape extends ShapeBase
	{
		public var textArea:TextArea;
		
		public function TextShape()
		{
			super();
			percentWidth = 100;
			percentHeight = 100;
		}
		
		override protected function updateDisplayList(w:Number, h:Number):void
		{
			textArea.width = w;
			textArea.height = h;
		}
		
		override protected function createChildren():void
		{
			textArea = new TextArea();
			textArea.percentHeight = 100;
			textArea.percentWidth = 100;
			textArea.setStyle('backgroundAlpha', 1);
			textArea.verticalScrollPolicy = 'off';
			textArea.horizontalScrollPolicy = 'off';
			textArea.setStyle("fontFamily","ArialEmbedded");
			textArea.editable=false;
			textArea.selectable=false;
			
			addChild(textArea);
			
			shapeHandles=new RectangleHandles();
			parentLayer.addChild(shapeHandles);
			
			var textSwitchButton:Button=new Button();
			textSwitchButton.label="Edit Text";
			textSwitchButton.width=75;
			textSwitchButton.height=20;
			textSwitchButton.toggle=true;
			textSwitchButton.setStyle("fontWeight","normal");
			textSwitchButton.addEventListener(MouseEvent.CLICK,function(event:Event):void
			{
				editingSubObject=textSwitchButton.selected;
				textArea.editable=textSwitchButton.selected;
				textArea.selectable=textSwitchButton.selected;
				textSwitchButton.label=(textSwitchButton.selected)?"Move":"Edit";
			});
			
			var rectHandles:RectangleHandles=shapeHandles as RectangleHandles;
			rectHandles.switchViewControls.addChild(textSwitchButton);
			rectHandles.switchViewControls.invalidateSize();
			rectHandles.switchViewControls.invalidateDisplayList();
			rectHandles.xToUse=x;
			rectHandles.yToUse=y;
			rectHandles.width=width;
			rectHandles.height=height;
			rectHandles.positionToCurrentShape();
			
			
			BindingUtils.bindProperty(shapeHandles,"visible",this,"selected");
			BindingUtils.bindProperty(this,"width",shapeHandles,"width");
			BindingUtils.bindProperty(this,"height",shapeHandles,"height");
			BindingUtils.bindProperty(this,"x",shapeHandles,"xToUse");
			BindingUtils.bindProperty(this,"y",shapeHandles,"yToUse");
			BindingUtils.bindProperty(this,"rotation",shapeHandles,"rotationToUse");
			
			
			shapeOptions = new TextOptions(textArea);
			BindingUtils.bindSetter(backgroundColorChanged,this,"color");
			BindingUtils.bindSetter(backgroundAlphaChanged,this,"fillAlpha");
			BindingUtils.bindSetter(lineThicknessChanged,this,"lineAlpha");
			BindingUtils.bindSetter(lineColorChanged,this,"lineColor");
		}
		private function backgroundColorChanged(value:uint):void
		{
			textArea.setStyle("backgroundColor",value);
		}
		private function backgroundAlphaChanged(value:Number):void
		{
			textArea.setStyle("backgroundAlpha",value);
		}
		private function lineThicknessChanged(value:Number):void
		{
			textArea.setStyle("borderThickness",value);
		}
		private function lineColorChanged(value:uint):void
		{
			textArea.setStyle("borderColor",value);
		}
		
	}
}